﻿IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'SearchOrdenCompra')
	BEGIN
		DROP  Procedure  SearchOrdenCompra
	END

GO

CREATE Procedure SearchOrdenCompra

	(
		@desde datetime,
		@hasta datetime
	)

AS

BEGIN
	SET NOCOUNT ON
	
	SELECT
	OC.[Descripcion],
	OC.[Estado],
	OC.[FechaCompra],
	OC.[IdEmpleado],
	OC.[IdOrdenCompra],
	OC.[IdOrdenPedido],
	OC.[IdProveedor],
	OP.[IdPedido],
	OP.[TipoOrdenPedido],
	P.[IdArea],
	A.[Nombre]
	FROM [OrdenCompra] AS OC
	INNER JOIN [OrdenPedido] AS OP ON OC.[IdOrdenPedido] = OP.[IdOrdenPedido]
	INNER JOIN [Pedido] AS P ON OP.[IdPedido] = P.[IdPedido]
	INNER JOIN [Area] AS A ON P.[IdArea] = A.[IdArea]
	WHERE OC.[FechaCompra] BETWEEN @desde AND @hasta 
	ORDER BY OC.[IdOrdenPedido] DESC

	SET NOCOUNT OFF
END

GO


GRANT EXEC ON SearchOrdenCompra TO PUBLIC

GO

